package com.hhs.storage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhs.storage.bean.InputParam;
import com.hhs.storage.bean.BaseRole;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.bean.BaseUser;
import com.hhs.storage.service.BaseLogsService;
import com.hhs.storage.service.BaseUserService;
import com.hhs.storage.util.Md5Util;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import liquibase.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@RestController
@RequestMapping("user")
@Slf4j
public class BaseUserController {
    @Resource
    private BaseLogsService baseLogsService;
    @Resource
    BaseUserService userService;
    @Autowired
    private HttpServletRequest request;
    @Resource
    private BaseUserService baseUserService;

    @RequestMapping(value = "table")
    public Map<String, Object> getAll(InputParam inputParam) {
//      baseLogsService.log("用户管理","查询",null);
        inputParam.mapParams("name","account","flag","dataType","targetId");
        return userService.getAll(inputParam);
    }
    @PostMapping(value = "edit")
    public Map<String, Object> saveOrUpdate(@Validated BaseUser user){
        log.info("新增或插入用户信息");
        if (org.springframework.util.StringUtils.isEmpty(user.getId())){
            baseLogsService.log("用户管理","新增",null);
        }else{
            baseLogsService.log("用户管理","编辑",null);
        }
        return userService.addOrUpdate(user,null);
    }
    @PostMapping(value = "editRole")
    //用户与角色的关联
    public Map<String, Object> editRole(String userId, String roleStr){
        if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(roleStr) ){
            return ResponseInfo.error("数据不能为空");
        }
        return userService.editRole(userId,roleStr.split(","));
    }
    @PostMapping(value = "changePass")
    public Map<String, Object> changePass(HttpServletRequest request, String oldPass,String newPass){
        baseLogsService.log("用户管理","修改密码",null);

        String createrId = SysUtil.getLoginUserId(request);
        if(StringUtils.isEmpty(createrId)){
            return ResponseInfo.error("账号不能为空");
        }
        return userService.changePass(createrId,oldPass,newPass);
    }
    @PostMapping(value = "resetPass")
    public Map<String, Object> resetPass(HttpServletRequest request, String id){
        baseLogsService.log("用户管理","重置密码",null);
        return userService.resetPass(id);
    }
    @PostMapping(value = "changeStatus")
    public Map<String, Object> changeStatus(BaseUser user){
        baseLogsService.log("用户管理","更改状态",null);

        if(StringUtils.isEmpty(user.getId()) ||
          user.getStatus()==null){
            return ResponseInfo.error("数据错误");
        }
        return userService.changeStatus(user);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Map<String, Object> dele(@PathVariable("id") String id) {
        log.info("删除用户");
        baseLogsService.log("用户管理","删除",null);
        return userService.dele(id);

    }
    @RequestMapping(value = "deleM")
    public Map<String, Object> deleM(String ids) {
        if(StringUtil.iszNull(ids)){
            return ResponseInfo.error("参数错误");
        }else{
            String[] idArr = ids.split(",");
            for (String id : idArr) {
                userService.dele(id);
            }
            return ResponseInfo.success(null);
        }
    }
    /**
     * 获取所有业务员
     * @return
     */
    @RequestMapping(value = "getUsersByRoleFlag")
    public Map<String, Object> getUsersByRoleFlag(@RequestParam String flag) {
        return userService.getUsersByRoleFlag(flag);
    }

    /**
     * 获取角色下所有业务员
     * @return
     */
    @RequestMapping(value = "getUsersByRoleIds")
    public Map<String, Object> getUsersByRoleIds(@RequestParam String roleIds) {
        return userService.getUsersByRoleIds(roleIds);
    }

//    @RequestMapping(value = "getAllById")
//    public Map<String, Object> getAllById(String userId) {
//        return userService.getAllById(userId);
//    }

    /*部门新增人员
    * user 用户信息
    * departId 部门id
    * employeeType 用户在部门内的类型  1 负责人  2 职员
    */
    @PostMapping(value = "addToDepart")
    public Map<String, Object> addToDepart( BaseUser user,String departId,Integer employeeType){
        if ( StringUtils.isEmpty(departId)){
            return ResponseInfo.error("数据错误");
        }
        if ( StringUtils.isEmpty(user.getId())){
            baseLogsService.log("用户管理","新增",null);
        }else{
            baseLogsService.log("用户管理","编辑",null);
        }
        return userService.addToDepart(user,"common",departId,employeeType);
    }

    //统计总共有多少用户——部门管理页面有用到
    @PostMapping(value = "sum")
    public Map<String, Object> sum( ){

        int count = userService.sum();
        return ResponseInfo.success(count);
    }
    @RequestMapping(value = "getInfo")
    public Map<String, Object> getInfo( ){
        return userService.getInfo(SysUtil.getLoginUserId(request));
    }

    /**
     * 获取老板信息
     * @return
     */
    @RequestMapping(value = "getBoss")
    public Map<String, Object> getBoss( ){
        return ResponseInfo.success(userService.getById(21));
    }

    //根据姓名模糊查询用户，在项目计划 按成员查询用到
    @RequestMapping(value = "getByName")
    public Map<String, Object> getByName( String name){
        return  userService.getByName(name);
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "getById")
    public Map<String, Object> getById(@RequestParam String id){
        return  ResponseInfo.success(userService.getById(id));
    }

    /**
     * @param inputParam
     * @return
     */
    @RequestMapping(value = "doPassStatus")
    public Map<String, Object> doPassStatus(InputParam inputParam){
        BaseUser byId = baseUserService.getById(inputParam.getUserId());
        if(!Md5Util.getMD5("123456").equals(byId.getPassword())){
            return ResponseInfo.success();
        }
        return  ResponseInfo.error("密码没有修改过");
    }
}

